<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>链表结构</title>
    * 只能从头部遍历到尾部或者从尾部遍历到头部（一般从头开始遍历）
    * 也就是链表相连的过程是单向的
    * 实现原理是链表中的元素都有上一个指向下一个的引用
</head>

<body>
    <script>
        class Element {
            constructor(data) {
                this.data = data
                this.next = null
            }
        }
        class LinkList {
            constructor(data) {
                
                this.head = null
                this.length = 0

            }
            append(data) {
                let nNode = new Element(data)
                if (this.head == null) {
                    this.head = nNode
                } else {
                    let current = this.head
                    while (current.next != null) {
                        current = current.next
                    }
                    current.next = nNode
                }
                this.length += 1
            }
            insert(data, position) {
                if (position < 0 || position > this.length) return false
                let nNode = new Element(data)
                if (position == 0) {
                    nNode.next = this.head
                    this.head = nNode
                } else {
                    let index = 0
                    let current = this.head
                    let previous = null
                    while (index++ < position) {
                        previous = current
                        current = current.next
                    }
                    nNode.next = current
                    previous.next = nNode
                    this.length += 1
                }
                return true
            }
            get(position) {
                if (this.position < 0 || this.position >= this.length) return null
                let index = 0
                while (index++ < position) {
                    current = current.next
                }
                return current.data
            }
            update(position, data) {
                if (this.position < 0 || this.position >= this.length) return null
                let index = 0
                while (index++ < position) {
                    current = current.next
                }
                current.data = data
                return true
            }
            removeAt(position) {
                if (position < 0 || position >= this.length) return null
                let current = this.head
                if (position == 0) {
                    this.head = this.head.next
                } else {
                    let previous = null
                    let index = 0
                    while (index++ < position) {
                        previous = current
                        current = current.next
                    }
                    current = previous.next
                }
                this.length -= 1
                return current.data
            }
            remove = function (element) {
                let position = this.indexOf(element)
                this.removeAt(position)
            }
            indexO(data) {
                let current = this.head
                let index = 0
                let isFind = false
                while (current) {
                    if (current.data == data) {
                        return index
                    }
                    current = current.next
                    index += 1
                }
                return -1
            }
            isEmpty() {
                return this.length == 0
            }
            size() {
                return this.length
            }
            toString(data) {
                let current = this.head
                let eString = ''
                while (current) {
                    eString += current.data + ''
                    current = current.next
                }
                return eString.slice(1)
            }
        }
        let linkList = new LinkList()
        linkList.append('abc')
        linkList.append('bca')
        linkList.append('cab')
        let position0 = linkList.removeAt(0)
        console.log(linkList)
        console.log(position0)
    </script>
</body>

</html>